home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / tpu2asm.zip / TPU2ASM.DOC < prev    next >
Text File  |  1990-10-29  |  16KB  |  313 lines

  1. ┌─────────────────────────────────────────────────────────────────────────────┐
  2. │                                                                             │
  3. │                                 Documentation for                           │
  4. │                                                                             │
  5. │                                    TPU2ASM.EXE                              │
  6. │                                                                             │
  7. │                            A symbolic disassembler for                      │
  8. │                          Turbo Pascal version 5.0 units                     │
  9. │                                                                             │
  10. │                       Copyright (C) 1989 by Per Bent Larsen                 │
  11. │                                All rights reserved                          │
  12. │                                                                             │
  13. │                              Version 1.0, March 1989                        │
  14. │                                                                             │
  15. └─────────────────────────────────────────────────────────────────────────────┘
  16.  
  17.  
  18. What it is
  19. ──────────────────────────────────────────────────────────────────────────────
  20. TPU2ASM is a symbolic disassembler capable of extracting compiled code from a
  21. version 5.0 Turbo Pascal TPU file. The output is a TASM compatible assembly
  22. file.
  23.  
  24.  
  25. What it can be used for
  26. ──────────────────────────────────────────────────────────────────────────────
  27. The utility can be used as an aid in developing EXTERNAL procedures and func-
  28. tions for Turbo Pascal, allowing the programmer to write a template function or
  29. procedure with parameter and local variable allocation set up automatically by
  30. the compiler.
  31.  
  32. You can also choose to write the entire routine in pascal and then do manual
  33. optimization on the code produced by the compiler. The code generated by Turbo
  34. Pascal leaves much room for improvement as you will soon find out.
  35.  
  36. Another use for the utility is studying the code generated by the compiler to
  37. determine which pascal constructs are preferable to others in terms of execu-
  38. tion speed and code size. You'll be surprised.
  39.  
  40. Finally this utility makes it theoretically possible to convert an entire pro-
  41. gram to assembler thus allowing you to link it with other languages like C or
  42. PROLOG. To do this you must have the Turbo Pascal Runtime Library Source avai-
  43. lable from Borland Int'l. It requires a conversion of the RTL into pure assem-
  44. bler. Be warned - this may not be a trivial process.
  45.  
  46.  
  47. What it isn't
  48. ──────────────────────────────────────────────────────────────────────────────
  49. This utility was not meant to be a reverse engineering tool, though it can be
  50. used as such to a limited extend. All references are shown symbolically and
  51. when a symbol is not known due to lack of information in the symbol tables, the
  52. program does not attempt to make a symbol of it's own. When TPU2ASM can not
  53. find the symbol for a reference, it is always because one ore more units were
  54. compiled with $D- or $L-. References that can not be resolved symbolically are
  55. left as ???? in the output file. Exceptions to this are items which are always
  56. unnamed like the CS constant block. These items are named by TPU2ASM.
  57.  
  58.  
  59. How to use it
  60. ──────────────────────────────────────────────────────────────────────────────
  61. The program is invoked from the DOS prompt as follows:
  62.  
  63.   TPU2ASM UnitName OutputFile [Procedure|Function]
  64.  
  65. The UnitName is of course the name of the unit to extract from. You should not
  66. specify an extension. The unit is assumed to be in a file with the same name
  67. and a TPU extension or in TURBO.TPL.
  68.  
  69. The OutputFile specifies where the assembly should go to. The extension de-
  70. faults to ASM. If the file exists you are prompted for overwrite permission.
  71.  
  72. The last parameter is an optional procedure or function name. If you omit this
  73. entry it is assumed that you wish to extract all procedures and functions from
  74. the unit. You are prompted for verification in this case. Only global procedu-
  75. res and functions can be extracted. This is because local routines always have
  76. an access link to the local data in the owner routine, more on that later. Glo-
  77. bal routines will pull all their local routines into the disassembly as well.
  78.  
  79.  
  80. Limitations
  81. ──────────────────────────────────────────────────────────────────────────────
  82. TPU2ASM reads the symbol table from all referenced units into RAM. This imposes
  83. a restriction on the number and size of units referenced. However I haven't
  84. encountered any heap overflows on my 640K machine even when extracting from
  85. very large units with many references. Only symbol tables are kept in RAM, not
  86. entire units.
  87.  
  88. Extracting EXTERNAL routines can not always be done correctly. A Turbo Pascal
  89. procedure or function is always stored in the TPU as a single block. If the
  90. entry point is zero, the entire block is known to be executable code, otherwise
  91. the first part of the block is known to be CS constants (literal data like
  92. strings, sets, range checking values and so on) and the rest is executable
  93. code. Not so with externals. OBJ files can have multiple routines in them and
  94. they can have data areas anywhere in both the data and code segments. Turbo
  95. Pascal does not keep track of anything but the data size, the code size (inclu-
  96. ding CS data) and the entry points for the PUBLIC routines. When extracting EX-
  97. TERNALS from a TPU, TPU2ASM initially assumes that the entire block up to the
  98. entry point is data. When it does the disassembly, it checks for jumps and
  99. calls into the assumed data part. If such a branch is encountered, the disas-
  100. sembly process is restarted with this new address as starting point. This pro-
  101. cess is repeated as required. The consequence of this strategy is that if the
  102. external module has multiple PUBLIC symbols, you may get the same routine in
  103. the output more than once. Also any data after the starting point is assumed
  104. to be code.
  105.  
  106. TPU2ASM attempts to create an ASM file which can be compiled directly by TASM.
  107. However TPU2ASM does not attempt to solve symbolic conflicts between Turbo
  108. Pascal and TASM. There are two types of errors likely to occur in an ASM file
  109. produced by TPU2ASM: Either you have used an identifier name in you pascal pro-
  110. gram which happens to be a reserved word in TASM, or you have local and global
  111. identifiers with duplicate names. TASM does not handle scope concepts as does
  112. pascal. Both types of errors can be dealt with by choosing other identifier
  113. names in the pascal source.
  114.  
  115.  
  116. Symbols defined by TPU2ASM
  117. ──────────────────────────────────────────────────────────────────────────────
  118. When disassembling a code or data reference, TPU2ASM uses the symbol from the
  119. pascal source if it's available. Data and code external to the unit is referen-
  120. ced by the pascal symbol directly. Parameters and local variables have a BP
  121. EQUate immediately following the PROC header, and all subsequent references are
  122. done with the EQU symbol. All local variables and labels are preceded with @@
  123. to avoid conflict with other PROCs. When disassembling functions, TPU2ASM de-
  124. fines a BP EQUate to $RESULT which denotes the function result buffer. When
  125. disassembling local routines, an additional BP symbol $LINK is defined, which
  126. denotes the Base Page link to the calling routines local data. Local label
  127. names are defined as @@<number>: , the exception being when call or jumps out-
  128. side the PROC-ENDP pair is encountered (this can only occur in EXTERNALS). In
  129. this case the label names are defined as <proc name><number>$. If the unit has
  130. initialization code, it is given the name <UnitName>$INIT.
  131.  
  132. TASM offers a TPASCAL model directive to simplify creation of EXTERNALs for
  133. Turbo Pascal. TPU2ASM does not use it, however. This is to give you complete
  134. flexibility to change the code. When you use TPASCAL, TASM always sets up a
  135. so called base frame, which is not always necessary.
  136.  
  137.  
  138. References to the SYSTEM unit
  139. ──────────────────────────────────────────────────────────────────────────────
  140. The SYSTEM unit in TURBO.TPL has special properties. Only a few of the SYSTEM
  141. routines listed in the manual or on the on line help screens for the unit actu-
  142. ally correspond to a routine in SYSTEM.TPU. Some routines like WRITELN and
  143. READLN produce a _number_ of calls to different built in SYSTEM routines depen-
  144. ding on the actual variables being written or read. Others like LENGTH, SIZEOF,
  145. SEG, OFS, PTR, HI, LO etc are coded inline by the compiler. Yet other routines
  146. are service routines not described in the documentation. Among these are routi-
  147. nes for range- and stack checking, program initialization and error reporting.
  148.  
  149. The problem with the SYSTEM unit in relation to TPU2ASM is, that it does not
  150. interface any procedures or functions at all. The compiler has a symbol table
  151. for the SYSTEM unit built into it, and the Turbo Pascal smart linker is smart
  152. enough to recognize calls to these routines. In order to be able to solve refe-
  153. rences to built in SYSTEM procedures, TPU2ASM too has a built in symbol table
  154. of all SYSTEM procedures. Unfortunately this only solves half of the problem.
  155. Having the names of the SYSTEM procedures makes it possible for TPU2ASM to show
  156. calls and jumps to these routines, but because the symbols are not present in
  157. SYSTEM.TPU (as they are not interfaced), the linker can not resolve these refe-
  158. rences. To draw your attention to this, TPU2ASM places a # in front of all
  159. calls and jumps to SYSTEM routines. There are two ways out of this problem:
  160.  
  161. The simplest solution is to avoid direct access to SYSTEM routines. You can
  162. create a shell unit, which exports routines with the same names as the SYSTEM
  163. procedures you wish to use. Example:
  164.  
  165.  
  166. Unit Shell;
  167. Interface
  168.     PROCEDURE GetMem(VAR P;Size:WORD);
  169.     PROCEDURE WriteLn(S:STRING);
  170.  
  171. Implementation
  172.     PROCEDURE GetMem(VAR P;Size:WORD);
  173.     BEGIN
  174.       SYSTEM.GetMem(Pointer(P),Size);
  175.     END;
  176.  
  177.     PROCEDURE WriteLn(S:STRING);
  178.     BEGIN
  179.       SYSTEM.WriteLn(S);
  180.     END;
  181.  
  182.     e.t.c.
  183.  
  184. The routines in the shell unit can be called freely from both pascal and assem-
  185. bler programs.
  186.  
  187. The other approach requires that you have the Turbo Pascal Runtime Library
  188. Source, available from Borland Int'l. You must change the SYSTEM.PAS to inter-
  189. face all procedures. Then you must re-compile the TURBO.TPL using the MAKEFILE
  190. supplied with the package. This will not only allow TPU2ASM to solve all refe-
  191. rences to SYSTEM.TPU just like it solves references to all other units, namely
  192. by doing a lookup in the units symbol table, it will also allow you to call the
  193. routines directly from your EXTERNALS. You should be aware, however that chan-
  194. ging and recompiling TURBO.TPL will invalidate _all_ TPUs created before the
  195. change. This is especially a problem with units for which you don't have the
  196. source code.
  197.  
  198.  
  199. Unit search strategy
  200. ──────────────────────────────────────────────────────────────────────────────
  201. Apart from the unit containing the code, TPU2ASM must be able to locate all
  202. units that contain definitions used by the code unit. When invoked, TPU2ASM
  203. looks for TPC.CFG or TURBO.TP in 1) the current directory, 2) the DOS path, 3)
  204. the TPU2ASM directory (DOS 3+ only). If either file is found, the Unit Direc-
  205. tories definition is read and appended to the search path used internally by
  206. TPU2ASM. When attempting to locate a unit, TPU2ASM uses the same search strate-
  207. gy as mentioned for TPC.CFG/TURBO.TP. TPU2ASM does not look at the Turbo Direc-
  208. tory or in TURBO.EXE for search paths. If you get a "unit not found" message,
  209. you should create a TURBO.TP or TPC.CFG file specifying the Unit Directory in
  210. your current directory.
  211.  
  212.  
  213. System requirements
  214. ──────────────────────────────────────────────────────────────────────────────
  215. You'll need at least 128K RAM and DOS 2.0 or later to run TPU2ASM. To assemble
  216. the output file you'll need TASM 1.0 or later.
  217.  
  218.  
  219. Disclaimer of warranty
  220. ──────────────────────────────────────────────────────────────────────────────
  221. This software and it's documentation are sold as is without any express or im-
  222. plied warranties whatsoever. The programs relies heavily upon the internal
  223. structure of TPU files generated by the Turbo Pascal version 5.0 compiler. No
  224. information has been made available officially or unofficially about this for-
  225. mat by Borland Int'l. Because of this and because of the diversity of condi-
  226. tions under which this program may be used, no warranty of fitness for a parti-
  227. cular purpose is offered. I do not assume any liability for the use of this
  228. program beyond the original purchase price. In no event will I be liable to you
  229. for any lost profits or other damages arising out of the use of this program,
  230. even if I have been advised of the possibility of such.
  231.  
  232.  
  233. How to register
  234. ──────────────────────────────────────────────────────────────────────────────
  235. This program is sold under the ShareWare concept. This means that you may keep
  236. a copy of it for a period of 30 days for the sole purpose of testing it. After
  237. you have reviewed it, you should register it if you plan to continue using it.
  238. As a benefit of registration, you will receive the full source code for TPU2ASM
  239. (requires Turbo Professional 5.0 to compile) plus a few public domain utilities
  240. for Turbo Pascal, also written by me. You are encouraged to distribute the sha-
  241. reable version of TPU2ASM along with this documentation to others via BBS or on
  242. diskette.
  243.  
  244. The price for the TPU2ASM package is $25 (or DKR. 175), including P&H.
  245.  
  246. The entire profit gained from selling this program will go into developing
  247. other utilities for Turbo Pascal.
  248.  
  249. You can register with me directly by sending check or money to
  250.  
  251.  
  252.         Per Bent Larsen
  253.         Vesterled 24
  254.         8600 Silkeborg
  255.         DK-Denmark
  256.  
  257. or you can register with the Public (Software) Library (PSL) by calling
  258. 1-800-2424-PSL or by writing
  259.  
  260.         PSL
  261.         P.O. Box 35707
  262.         Houston
  263.         TX 77235-5705
  264.  
  265. MC/Visa Accepted.
  266.  
  267. Please specify 5 1/4 inch or 3 1/2 inch disk.
  268.  
  269.  
  270. Acknowledgements
  271. ──────────────────────────────────────────────────────────────────────────────
  272. TPU2ASM was originally based on the source code for the TINFO utility, which is
  273. part of the famous Analyst package created and marketed by TurboPower Software.
  274.  
  275. Without the knowledge and cooperation of the TurboPower people and Brian Foley
  276. in particular, this would not have been possible.
  277.  
  278.  
  279. Product names and Copyrights
  280. ──────────────────────────────────────────────────────────────────────────────
  281. Parts of the source code for TPU2ASM is copyrighted by TurboPower software and
  282. used by permission.
  283.  
  284. Analyst is a trademark of TurboPower Software.
  285. Turbo Professional is a registered trademark of Sunny Hill software licensed to
  286. TurboPower Software.
  287. Turbo Pascal and TASM are trademarks or registered trademarks of Borland
  288. International.
  289.  
  290.  
  291. Comments and suggestions
  292. ──────────────────────────────────────────────────────────────────────────────
  293. Feel free to contact me if you have comments or suggestions to this product or
  294. perhaps any future product, you would like to see. I can be reached on the
  295. address mentioned above or on CompuServe 75470,1320. Just drop a note in the
  296. Borland programming forum A (Go BPROGA), or send an E-Mail. In Europe I can
  297. also be reached via Borland Scandinavia's BBS as Per Larsen (+45 02 14 04 94,
  298. 1200/2400,N,8,1).
  299.  
  300. ──────────────────────────────────────────────────────────────────────────────
  301. Happy computing
  302.  
  303. Per B Larsen
  304.  
  305. 3/10/89
  306.  
  307. ──────────────────────────────────────────────────────────────────────────────
  308. Typing "TPU2ASM > TPU2ASM.DOC" at the DOS prompt will extract this information
  309. to the file TPU2ASM.DOC.
  310.  
  311. This is a non registered evaluation copy. The standard version comes with the
  312. documentation stored in a separate file.
  313.